package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.RoomPrice;
import com.yuyou.fn.educational.entity.SummerWinterDate;
import com.yuyou.fn.educational.mapper.RoomPriceMapper;
import com.yuyou.fn.educational.service.IRoomPriceService;
import com.yuyou.fn.educational.vo.RoomPriceVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 *
 * RoomPrice 表数据服务层接口实现类
 *
 */
@Service
public class RoomPriceServiceImpl extends SuperServiceImpl<RoomPriceMapper, RoomPrice,Long> implements IRoomPriceService {

    @Override
    @Transactional
    public void addByRoomPriceVo(RoomPriceVo roomPriceVo) {
        RoomPrice roomPrice=new RoomPrice();
        BeanUtils.copyProperties(roomPrice,roomPriceVo);;
        roomPrice.setUpdateTime(TimeUtils.now());
        if (roomPrice.getRoomId()==null || roomPrice.getSummerWinterDateId()==null){
            throw new BusException("课室或者寒暑假时间段未选择");
        }
        RoomPrice check=this.findByRoomIdAndYear(roomPriceVo.getRoomId(),roomPriceVo.getYear());
        if (check!=null){
            roomPrice.setRoomPriceId(check.getRoomPriceId());
            this.updateById(roomPrice);
        }else {
            roomPrice.setCreateTime(TimeUtils.now());
            this.insert(roomPrice);
        }
    }

    @Override
    public RoomPrice findByRoomIdAndYear(Long roomId, String year) {
        Example<RoomPrice> example=RoomPrice.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("roomId",roomId);
        criteria.andEqualTo("year",year);
        return this.findOneByExample(example);
    }

    @Override
    public void changeDateBySummerWinterDate(SummerWinterDate summerWinterDate) {
        Example<RoomPrice> example=RoomPrice.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("summerWinterDateId",summerWinterDate.getSummerWinterDateId());
        RoomPrice roomPrice=new RoomPrice();
        roomPrice.setSummerStartDate(summerWinterDate.getSummerStartDate());
        roomPrice.setSummerEndDate(summerWinterDate.getSummerEndDate());
        roomPrice.setWinterStartDate(summerWinterDate.getWinterStartDate());
        roomPrice.setWinterEndDate(summerWinterDate.getWinterEndDate());
        roomPrice.setUpdateTime(TimeUtils.now());
        this.updateSelectiveByExample(roomPrice,example);
    }

    @Override
    public List<RoomPrice> findByYearsAndRoomIds(Set<String> years, Set<Long> roomIds) {
        if (years==null || years.size()<=0 ||roomIds==null || roomIds.size()<=0){
            return Collections.EMPTY_LIST;
        }
        Example<RoomPrice> example=RoomPrice.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("roomId",roomIds);
        criteria.andIn("year",years);
        return this.findListByExample(example);
    }
}